import requests
import re

headers = {
    'referer': 'https://www.kugou.com/',
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit'
                 '/537.36 (KHTML, like Gecko) Chrome'
                 '/130.0.0.0 Safari/537.36'
}

url = 'https://wwwapi.kugou.com/play/songinfo'

# 查询参数
data = {
    'srcappid': '2919',
    'clientver': '20000',
    'clienttime': '1732437362719',
    'mid': 'b7380c996e5c29ccd1ed4307ee86322a',
    'uuid': 'b7380c996e5c29ccd1ed4307ee86322a',
    'dfid': '0R6vxL13Zpp44fBkqV37les1',
    'appid': '1014',
    'platid': '4',
    'encode_album_audio_id': 'bjpdyve5',
    'token': '',
    'userid': '0',
    'signature': 'c8697c1df9c6d872d5345897c0ef1a73',
}

data2 = {
    'srcappid': '2919',
    'clientver': '20000',
    'clienttime': '1732447487267',
    'mid': 'b7380c996e5c29ccd1ed4307ee86322a',
    'uuid': 'b7380c996e5c29ccd1ed4307ee86322a',
    'dfid': '0R6vxL13Zpp44fBkqV37les1',
    'appid': '1014',
    'platid': '4',
    'encode_album_audio_id': 'j410q60',
    'token': '',
    'userid': '0',
    'signature': '3c0b4f560c4d7e5bc4cc8a60be03a883',
}

srcappid: 2919
clientver: 20000
clienttime: 1732447487267
mid: b7380c996e5c29ccd1ed4307ee86322a
uuid: b7380c996e5c29ccd1ed4307ee86322a
dfid: 0R6vxL13Zpp44fBkqV37les1
appid: 1014
platid: 4
encode_album_audio_id: j410q60
token:
userid: 0
signature: 3c0b4f560c4d7e5bc4cc8a60be03a883



# 发送请求
response = requests.get(url=url, params=data2, headers=headers)

# 获取相应的json数据：返回字典数据
json_data = response.json()
print(json_data)

"""解析数据"""

# 提取歌曲名字
audio_name = json_data['data']['audio_name']

# 提取歌曲链接
play_url = json_data['data']['play_url']

print(audio_name, play_url)

"""保存数据"""
# 获取歌曲内容
music_content = requests.get(url=play_url, headers=headers).content # 获取二进制数据

# 数据保存
with open('music\\' + audio_name + '.mp3', 'wb') as f:
    f.write(music_content)